package com.bean;

import org.apache.hadoop.io.WritableComparable;

import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;

public class YearTemp implements WritableComparable<YearTemp> {
    private String year;
    private int temp;

    public YearTemp() {
    }

    public YearTemp(String year, int temp) {
        this.year = year;
        this.temp = temp;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }

    public int getTemp() {
        return temp;
    }

    public void setTemp(int temp) {
        this.temp = temp;
    }

    @Override
    public String toString() {
        return "YearTemp{" +
                "year='" + year + '\'' +
                ", temp=" + temp +
                '}';
    }

    @Override
    public int compareTo(YearTemp o) {
        // compareTo前面的比compareTo后面的小的时候，
        // compareTo返回-1，
        // compareTo后面就要放到compareTo前面的后面
        // 1999         2000
        // year1比year2大的时候，compareTo返回1
        // year2就要放到year1的前面
//        this.year=1999        o.getYear()=2000
        // n=-1            2000就要放到1999的后面
        // n=1              放前面
        int n = this.getYear().compareTo(o.getYear());
        if (n==0){
            // this.getTemp()=30    o.getTemp()=31
            // 31放到30后面
            return this.getTemp()-o.getTemp();
        }
        // 默认是升序，添加负号设置为降序排序
        return -n;
    }

    @Override
    public void write(DataOutput out) throws IOException {
        out.writeUTF(year);
        out.writeInt(temp);
    }

    @Override
    public void readFields(DataInput in) throws IOException {
        this.year = in.readUTF();
        this.temp = in.readInt();
    }
}
